<template>
  <div>
      <!-- 头部面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">控制台</el-breadcrumb-item>
      <el-breadcrumb-item><b>停车位管理</b></el-breadcrumb-item>
      <el-breadcrumb-item>车位使用管理</el-breadcrumb-item>
    </el-breadcrumb>
    <hr>

      <!-- 头部的卡片 -->
    <el-card style="font-size: 20px;font-family: 'Helvetica Nee',serif">
      <b>车位使用管理</b>
      <p style="font-size: 15px">用于管理小区停车场车位使用情况，包括车位上车辆牌照、车辆所有人、车位租售信息、车位使用时间和收费情况等信息</p>
    </el-card>
      <!-- 数据相关的卡片 -->
    <el-card class="box-card">
      <div class="card-header">
        <el-row>
            <!-- 添加按钮 -->
          <el-col :span="18">
              <Button type="success" icon="md-add" @click="dialogFormVisibleAdd = true">添加</Button>
              <span style="margin-left: 10px;"></span>
              <Button v-if="showDeleteButton" type="error" icon="md-trash" @click="deleteByIds()">批量删除</Button>
          </el-col>
            <!-- 搜索框 -->
            <el-col :span="4">
                <Input search style="width: 260px;" enter-button placeholder="请输入搜索的内容" @on-search="search()"/>
            </el-col>
            <!-- 根据小区查询下拉框 -->
            <el-col :span="2" style="text-align: right;">
                <Dropdown @on-click="selectByCommunity">
                    <Button type="primary">
                        根据小区查询 <Icon type="ios-arrow-down"></Icon>
                    </Button>
                    <DropdownMenu slot="list">
                        <DropdownItem :name="null">
                            所有小区
                        </DropdownItem>
                        <DropdownItem v-for="community, index in communityList" :key="index" :name="community.code">
                            {{ community.name }}
                        </DropdownItem>
                    </DropdownMenu>
                </Dropdown>
            </el-col>
        </el-row>
      </div>

        <!-- 数据表格 -->
      <el-table v-loading="loadTable" :border="true" ref="multipleTable" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column fixed prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="communityName" label="所属小区" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="parkingName" label="车位名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="licensePlate" label="车辆牌照" width="100"></el-table-column>
        <el-table-column prop="owner" label="车辆所有人" width="150"></el-table-column>
        <el-table-column prop="telephone" label="联系方式" width="120"></el-table-column>
        <el-table-column prop="type" label="使用性质" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{scope.row.type == 1 ? '已售' : '已租'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="cost" label="总费用（元）" width="120"></el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="180"></el-table-column>
        <el-table-column prop="stopTime" label="结束时间" width="180"></el-table-column>
        <el-table-column prop="gmtCreate" label="创建时间" width="180"></el-table-column>
        <el-table-column align="center" label="操`作" width="120" fixed="right">
            <template slot-scope="scope">
                <ButtonGroup>
                    <Button type="info" icon="md-create" @click="showUpdateDialog(scope.row)"></Button>
                    <Button type="error" icon="md-trash" @click="deleteIds = [scope.row.id]; deleteByIds()"></Button>
                </ButtonGroup>
            </template>
        </el-table-column>
      </el-table>

        <div class="block">
            <!-- 分页查询 -->
            <Page :total="total" show-elevator show-sizer :current="currentPage" :page-size="pageSize" @on-change="changePage" @on-page-size-change="changePageSize"/>
        </div>

        <!-- 编辑弹窗 -->
        <el-dialog title="编辑" :visible.sync="dialogFormVisibleUpdate" center>
            <el-form :model="editFormUpdate">
                <el-form-item label="所属小区" required>
                    <el-select v-model="editFormUpdate.communityName" placeholder="请选择" border>
                        <el-option v-for="item, index in communityList" :key="index" :label="item.name" :value="item.code">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车位编号" required>
                    <el-select v-model="editFormUpdate.parkingName" placeholder="请选择" border>
                        <el-option v-for="item, index in parkingList" :key="index" :label="item.name" :value="item.code">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车辆牌照">
                    <el-input v-model="editFormUpdate.licensePlate" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="车辆所有人" >
                    <el-input v-model="editFormUpdate.owner" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="editFormUpdate.telephone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="使用性质">
                    <el-input v-model="editFormUpdate.type" autocomplete="off">
                        <template>
                            <el-radio v-model="radio" label="1">租</el-radio>
                            <el-radio v-model="radio" label="2">售</el-radio>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="总费用（元）" >
                    <el-input v-model="editFormUpdate.cost" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="开始时间">
                  <el-date-picker v-model="editFormAdd.startTime" type="datetime" placeholder="选择时间"></el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间">
                  <el-date-picker v-model="editFormAdd.stopTime" type="datetime" placeholder="选择时间"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="update()">确 定</el-button>
                <el-button @click="dialogFormVisibleUpdate = false">关 闭</el-button>
            </div>
        </el-dialog>

        <!-- 添加弹窗 -->
        <el-dialog title="添加" :visible.sync="dialogFormVisibleAdd" center>
            <el-form :model="editFormAdd">
                <el-form-item label="所属小区" required>
                      <el-select v-model="editFormAdd.communityCode" placeholder="请选择" border>
                        <el-option v-for="item, index in communityList" :key="index" :label="item.name" :value="item.code">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车位编号" required>
                    <el-select v-model="editFormAdd.parkingName" placeholder="请选择" border>
                        <el-option v-for="item, index in parkingList" :key="index" :label="item.name" :value="item.code">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车辆牌照">
                    <el-input v-model="editFormAdd.licensePlate" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="车辆所有人">
                    <el-input v-model="editFormAdd.owner" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="editFormAdd.telephone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="使用性质">
                    <el-input v-model="editFormAdd.type" autocomplete="off">
                        <template>
                            <el-radio v-model="radio" label="1">租</el-radio>
                            <el-radio v-model="radio" label="2">售</el-radio>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="总费用（元）">
                    <el-input v-model="editFormAdd.cost" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="开始时间">
                    <el-input v-model="editFormAdd.startTime" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="结束时间" >
                    <el-input v-model="editFormAdd.stopTime" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="add('editFormAdd')">确 定</el-button>
                <el-button @click="dialogFormVisibleAdd = false">关 闭</el-button>
            </div>
        </el-dialog>

    </el-card>
      <!-- 返回顶部按钮 -->
      <BackTop></BackTop>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "index",
  data() {
    return {
        radio: '1',
        loadTable: false,
        showDeleteButton: false,
        deleteIds: [],
        editFormUpdate: {},
        editFormAdd: {},
        dialogFormVisibleUpdate: false,
        dialogFormVisibleAdd: false,
        communityList: [],
        parkingList: [],
        communityCode: null,
        searchText: "",
        tableData: [],
        currentPage: 1,
        pageSize: 10,
        total: 0
    }
  },
  methods: {
      // 搜索方法
      search() {

      },
      // 添加数据方法
      add() {
          if (Object.keys(this.editFormAdd).length === 0) {
              this.$message.error("请填写内容！");
              return;
          }
          let url = this.host + "/parkingUse/add";
          this.axios.post(url, JSON.stringify(this.editFormAdd), {
              headers: {
                  "Content-Type": "application/json"
              }
          }).then((response) => {
              if (response.data.statusCode == 2000) {
                  this.$message.success(response.data.data);
                  this.dialogFormVisibleAdd = false;
                  this.loadDataList();
              } else {
                  this.$message.error(response.data.message);
              }
          });
      },
      // 批量删除
      deleteByIds() {
          // 打开加载状态
          this.loadTable = true;
          let url = this.host + "/parkingUse/delete?" + this.deleteIds.map(item => `ids=${encodeURIComponent(item)}`).join('&');
          this.axios.get(url).then((response) => {
              if (response.data.statusCode == 2000) {
                  this.$message.success(response.data.data);
                  this.loadDataList();
              } else {
                  this.$message.error("删除失败，请稍后再试！");
              }
          });
          // 取消加载状态
          this.loadTable = false;
          this.deleteIds = [],
              this.showDeleteButton = false;
      },
      // 表格多选选择后触发的方法
      handleSelectionChange(rows) {
          if (rows.length === 0) {
              this.showDeleteButton = false;
              return;
          }
          this.showDeleteButton = true;
          for (let item of rows) {
              this.deleteIds.push(item.id);
          }
      },
      // 打开弹窗方法
      showUpdateDialog(row) {
          this.dialogFormVisibleUpdate = true;
          this.editFormUpdate = row;
      },
      // 修改数据的方法
      update() {
          if (Object.keys(this.editFormUpdate).length === 0) {
              this.$message.error("请填写内容！");
              return;
          }
          let url = this.host + "/parkingUse/update";
          this.axios.post(url, JSON.stringify(this.editFormUpdate), {
              headers: {
                  "Content-Type": "application/json"
              }
          }).then((response) => {
              if (response.data.statusCode == 2000) {
                  this.$message.success(response.data.data);
                  this.dialogFormVisibleUpdate = false;
                  this.loadDataList();
              } else {
                  this.$message.error(response.data.message);
              }
          });
      },
      // 选择小区后调用的方法
      selectByCommunity(communityCode) {
          this.communityCode = communityCode;
          this.loadDataList();
      },
      // 改变第几页后调用的方法
      changePage(pageNum) {
          this.currentPage = pageNum;
          this.loadDataList();
      },
      // 改变每页多少条数据后调用的方法
      changePageSize(pageSize) {
          this.pageSize = pageSize;
          this.loadDataList();
      },
      // 加载当前模块数据
      loadDataList() {
          this.loadTable = true;
          let url = this.host + "/parkingUse/list?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize;
          if (this.communityCode != null) {
              url = this.host + "/parkingUse/list?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize + "&communityCode=" + this.communityCode;
          }
          this.axios.get(url).then((response) => {
              if (response.data.statusCode == 2000) {
                  this.tableData = response.data.data.records;
                  this.currentPage = response.data.data.current;
                  this.pageSize = response.data.data.size;
                  this.total = response.data.data.total;
              } else {
                  this.$message.error("加载数据出错了！");
              }
          });
          this.loadTable = false;
      },
      // 加载所有楼栋数据
      loadParkingList() {
          let url = this.host + "/parking/list";
          this.axios.get(url).then((response) => {
              if (response.data.statusCode == 2000) {
                  this.parkingList = response.data.data.records;
              } else {
                  this.$message.error("加载数据出错了！");
              }
          });
      },
      // 加载所有小区数据
      loadCommunityList() {
          let url = this.host + "/community/list";
          this.axios.get(url).then((response) => {
              if (response.data.statusCode == 2000) {
                  this.communityList = response.data.data.records;
              } else {
                  this.$message.error("加载数据出错了！");
              }
          });
      }
  },
    mounted() {
        // 页面初始化后调用加载本模块数据和小区等的所有数据
        this.loadDataList();
        this.loadCommunityList();
        this.loadParkingList();
    }
}
</script>

<style scoped>
.card-header {
  margin-bottom: 20px;
}

.block {
  text-align: center;
  margin-top: 10px;
}

.el-select {
  margin-left: 15px;
  margin-right: 30px;
}

.el-date-picker {
  margin-left: 50px;
}
</style>
